﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Utilities;
using StructureMap;

namespace MedicalCare.Admin
{
    public partial class BPList : System.Web.UI.Page
    {
        private const string OrderKey = "ReceiveTime";
        private const string OrderValue = "DESC";
        private const int PageSize = 10;
        private IBloodPressureAdaptor _bpAdaptor;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var whereString = "1=1";
                var test = Request.Params["Id"];
                test = Request["Id"];
                _bpAdaptor = ObjectFactory.GetInstance<IBloodPressureAdaptor>();
                hideOrderby.Value = OrderKey; // 0514
                pager1.AlwaysShow = true;
                pager1.PageSize = PageSize;
                pager1.RecordCount = _bpAdaptor.GetRecordsTotal(whereString);
                BindDataList(true,whereString);
            }
        }

        private void BindDataList(bool rebindPager, string whereString)
        {
            //var test1 = MD5.Encrypt("430523198508230033#asdffdsfa", MD5KeyType.MD5Key);
            //var test2 = MD5.Encrypt("430523198508230033#asdffdsfa", MD5KeyType.MD5Key);

            //var test = DESKeyType.MC2012ID.ToString();
            //test1 = DES.Encrypt("1231fdsfds000", DESKeyType.MC2012ID.ToString());
            //test2 = DES.Decrypt(test1, DESKeyType.MC2012ID.ToString());

           
            var currentPageSize = 1;
            try
            {
                if (Request.QueryString["Page"] != null)
                    currentPageSize = Convert.ToInt32(Request.QueryString["Page"]);
                else
                    currentPageSize = pager1.CurrentPageIndex;
            }
            catch
            {
                currentPageSize = 1;
            }

            var bpAdaptor = ObjectFactory.GetInstance<IBloodPressureAdaptor>();
            var source = bpAdaptor.GetPagingRecords(OrderKey, OrderValue, currentPageSize, PageSize, whereString);

            var objPds = new PagedDataSource();
            objPds.DataSource = source;
            objPds.AllowPaging = true;
            objPds.PageSize = PageSize;
            bpList.DataSource = objPds;
            bpList.DataBind();


            var lowBP = bpAdaptor.GetRecordsTotal(" DBP < 70 ");
            var HighBP = bpAdaptor.GetRecordsTotal(" SBP > 140 ");

            btnlowBP.Text = "低血压数据(" + lowBP + ")";
            btnHighBP.Text = "高血压数据(" + HighBP + ")";
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            var whereString = "1=1";
            if (cbAutoRefresh.Checked)
                BindDataList(true, whereString);
        }

        protected void msgPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            var whereString = "1=1";
            pager1.CurrentPageIndex = e.NewPageIndex;
            BindDataList(true, whereString);
        }

        // UI control
        protected object Formatlength_10(object str)
        {
            //if (str.ToString().Length > 1)
            //{
            //    str = str.ToString().Substring(0, 1) + "...";
            //}
            return str;
        }

        protected object Validate(object str)
        {
            //if (str.ToString().Length > 1)
            //{
            //    str = str.ToString().Substring(0, 1) + "...";
            //}
            return str;
        }



        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            var whereString = "1=1";
            BindDataList(true, whereString);
        }

        protected void bpList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            //if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            //{
            //    var bpData = ((BloodPressureEntity)e.Item.DataItem);
            //    if (bpData.SBP > 140)
            //        e.Item.ForeColor = Color.Red;
            //    else if (bpData.DBP < 60)
            //        e.Item.ForeColor = Color.Orange;
            //}
        }

        protected void btnHighBP_Click(object sender, EventArgs e)
        {
            var whereString = " SBP > 140";
            BindDataList(true, whereString);

        }

        protected void btnlowBP_Click(object sender, EventArgs e)
        {
            var whereString = " DBP < 70";
            BindDataList(true, whereString);

        }
    }
}